<template>
  <div class="answer-form">
    <VueDraggableNext v-model="list" @end="onEnd" handle=".handle" animation="200" ghostClass="answer-form__ghost"
      chosenClass="answer-form__chosen">
      <transition-group>
        <div class="answer-form__item" v-for="(item, index) in list" :key="`${item.id}_${index}`"
          :class="{ 'answer-form__no': index === 0 }">
          <span v-if="props.questionType !== '3' || item.optionLabel" class="answer-form__label">{{ item.optionLabel
          }}、</span>

          <el-input v-model="item.content" placeholder="请输入题目" :disabled="props.questionType === '3'" />

          <img class="answer-form__handle handle" :src="questionDr" />

          <el-checkbox v-model="item.rightState" size="large" @change="changeCheckbox(item)" />

          <el-icon v-show="questionType !== '3'" size="17" @click="removeItem(item, index)">
            <Close />
          </el-icon>
        </div>
      </transition-group>
    </VueDraggableNext>
  </div>
</template>

<script setup>
import { VueDraggableNext } from "vue-draggable-next";

import { questionDr } from "../../assets/images.js";

const props = defineProps({
  list: {
    type: Array,
    required: true,
    default: () => [],
  },
  questionType: {
    type: String,
    default: 1,
  },
  form: {
    type: Object,
    default: () => ({}),
  },
});
const emit = defineEmits(["update", "remove"]);

let list = ref(props.form.questionAnswerList);
watch(
  () => list.value,
  (value) => {
    console.log("watch-- list");
    emit("update", value);
  }
);

if (!(list.value && list.value.length) && `${props.questionType}` !== "3") {
  list.value = [
    {
      content: "",
      rightState: false,
      optionLabel: "A",
      id: new Date().getTime(),
    },
  ];
}

if (
  `${props.questionType}` === "3" &&
  props.form.rightAnswers &&
  props.form.rightAnswers.length
) {
  const rightAnswersArr = JSON.parse(props.form.rightAnswers);

  list.value = [
    {
      optionLabel: "",
      content: "正确",
      rightState:
        rightAnswersArr &&
        rightAnswersArr.length &&
        rightAnswersArr[0] === "正确",
    },
    {
      optionLabel: "",
      content: "错误",
      rightState:
        rightAnswersArr &&
        rightAnswersArr.length &&
        rightAnswersArr[0] === "错误",
    },
  ];
}

let copyList = ref([]);

function removeItem(row, index) {
  list.value.splice(index, 1);
  onEnd();
}

function onEnd() {
  const arr = [];
  for (let i = 0; i < list.value.length; i++) {
    const item = list.value[i];
    arr.push({
      ...item,
      optionLabel: String.fromCharCode("A".charCodeAt() + i),
    });
  }
  list.value = arr;
}

function changeType(type) {
  console.log("🚀 ~ changeType ~ type:", type)
  if (type === 3) {
    copyList.value = [...list.value];
    list.value = [
      {
        content: "正确",
        rightState: true,
        optionLabel: "",
        id: new Date().getTime(),
      },
      {
        content: "错误",
        rightState: false,
        optionLabel: "",
        id: new Date().getTime() + 3 * 800 * 2,
      },
    ];
    return;
  }

  if (copyList.value && copyList.value.length) {
    list.value = [...copyList.value];
    return;
  }

  list.value = [];
  addItem();
}

function addItem() {
  const currentLabel = list.value.length || 0;

  list.value.push({
    content: "",
    rightState: false,
    optionLabel: String.fromCharCode("A".charCodeAt() + currentLabel),
    id: new Date().getTime(),
  });
}

function changeCheckbox(item) {
  if (`${props.questionType}` === "1" || `${props.questionType}` === "3") {
    list.value = list.value.map((row) => {
      if (item.content === row.content) return row;
      return { ...row, rightState: false };
    });
  }
}

const getQuestionAnswerList = () => {
  return list.value;
};

defineExpose({
  changeType,
  addItem,
  getQuestionAnswerList,
});
</script>
<style lang="scss" scoped>
.chosen {
  background-color: #000 !important;
  color: #fff;
}

.ghost {
  background-color: red !important;
}

:deep .el-checkbox {
  margin-right: 12px !important;
}

.answer-form {
  display: block;
  width: 100%;
  font-family: Microsoft YaHei;
  color: #606266;
  font-size: 14px;
  width: 600px;

  &__item {
    display: flex;
    align-items: center;
    flex-direction: row;
    margin-top: 12px;
  }

  &__label {
    font-weight: 400;
    color: #606266;
  }

  &__handle {
    width: 12px;
    height: 12px;
    margin: 0 16px;
  }

  &__close {
    margin-left: 16px;
  }

  &__no {
    margin: 0;
  }
}
</style>
